using System;
using UnityEngine;
using Utils.Extends;

namespace Utils.Events
{
    public class DestroyEventHandler : MonoBehaviour
    {
        public event Action OnDestroyHandler;

        private void OnDestroy()
        {
            OnDestroyHandler?.Invoke();
        }
    }

    public static class DestroyEventExtensions
    {
        public static void UnregisterOnDestroy(this IEventUnregister unregister, Component comp)
        {
            DestroyEventHandler handler = comp.GetOrAddComponent<DestroyEventHandler>();
            handler.OnDestroyHandler += unregister.Unregister;
        }
    }
}